package com.example.utils;

import com.mysql.cj.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/*
 *  @author 雨安
 *  类名： RedisCache
 *  创建时间：2024/3/18
 */
@SuppressWarnings(value = {"unchecked","rawtypes"})
@Component
public class RedisCache {
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 缓存基本对象：Integer,String 实体类等
     */
    public <T> void setCacheObject(final String key,final T value){
        //设置redis数据库
        redisTemplate.opsForValue().set(key,value);
    }

    /**
     * 缓存基本的对象：Integer,String 实体类等
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,value,timeout,timeUnit);
    }

    /**
     * 设置有效时间
     */
    public boolean expire(final String key,final long timeout){
        //返回
        return expire(key,timeout, TimeUnit.SECONDS);
    }

    /**
     * 设置有效时间
     */
    public boolean expire(final String key,final long timeout,final TimeUnit unit){
        return redisTemplate.expire(key,timeout,unit);
    }

    /**
     * 获得缓存的基本对象
     */
    public <T> T getCacheObject(final String key){
        //创建接收数据对象
        ValueOperations<String,T> operation =redisTemplate.opsForValue();
        //返回
        return operation.get(key);
    }


    /**
     * 删除单个对象
     */
    public boolean deleteObject(final String key){
        //返回删除的boolean值
        return redisTemplate.delete(key);
    }

    /**
     * 删除集合对象
     */
    public long deleteObject(final Collection collection){
        return redisTemplate.delete(collection);
    }

    /**
     * 缓存List数据
     */
    public <T> long setCacheList(final String key, final List<T> dataList){
        //接收成功影响行数
        Long count = redisTemplate.opsForList().rightPushAll(key,dataList);
        //返回count
        return count == null ? 0 : count;
    }

    /**
     * 获取缓存的list对象
     */
    public <T> List<T> getCacheLsit(final String key){
        //拿到全部数据
        return redisTemplate.opsForList().range(key,0,-1);
    }

    /**
     * 缓存Set集合
     */
    public <T>BoundSetOperations<String,T> setCacheSet(final String key, final Set<T> dataSet){
        //创建对象
        BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);
        //创建集合
        Iterator<T> it = dataSet.iterator();
        //遍历
        while (it.hasNext()){
            //添加
            setOperation.add(it.next());
        }
        //返回
        return setOperation;
    }

    /**
     * 获取缓存的set对象
     */
    public <T> Set<T> getCacheSet(final String key) {
        //返回拿到的对象
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 缓存Map
     */
    public <T> void setCacheMap(final String key, final Map<String,T> dataMap){
        //进行非空判断
        if (dataMap != null){
            redisTemplate.opsForHash().putAll(key,dataMap);
        }
    }

    /**
     * 获取缓存的Map
     */
    public <T> Map<String,T> getCacheMap(final String key){
        //返回redis中的map
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 往Hash中存入数据
     */
    public <T> void setCacheMapValue(final String key,final String hkey,final T value){
        redisTemplate.opsForHash().put(key,hkey,value);
    }

    /**
     * 获取Hash中的数据
     */
    public <T> T getCacheMapvalue(final String key,final String hkey){
        //创建
        HashOperations<String,String,T> opsForHash = redisTemplate.opsForHash();
        //返回
        return opsForHash.get(key,hkey);
    }


    /**
     * 删除Hash中的数据
     */
    public void delCacheMapValue(final String key,final String hkey){
        HashOperations hashOperations = redisTemplate.opsForHash();
        //删除
        hashOperations.delete(key,hkey);
    }

    /**
     * 获取多个Hash中的数据
     */
    public <T> List<T> getMultiCacheMapValue(final String key,final Collection<Object> hkeys){
        //返回数据集合
        return redisTemplate.opsForHash().multiGet(key,hkeys);
    }

    /**
     * 获取缓存的基本对象列表
     */
    public Collection<String> keys(final String pattern){
        return redisTemplate.keys(pattern);
    }

}
